package product;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class MemberSelect extends JInternalFrame {

	DefaultTableModel model;
	TableRowSorter<TableModel> tSoter;
	ResultSet rs = null;
	ResultSetMetaData rsmd = null;
	PreparedStatement pstmt = null;
	Vector<String> v = null;

	MemberUpdate u;
	MemberInput i;

	Connection conn;
	ProductMain pm;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;


	public MemberSelect(Connection conn, ProductMain pm) {
		super("멤버 조회", true, true, true, false);
		this.conn=conn;
		this.pm=pm;
		setBounds(100, 100, 450, 300);
		getContentPane().add(getBtnNewButton(), BorderLayout.NORTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);

	}
	
	public void setD(MemberUpdate u) {
		this.u = u;
	}
	public void setI(MemberInput i) {
		this.i = i;
	}


	public void columnadd() {
		if (table!=null) table=null;

		try {    

			String sql = "select * from memberdata";
			pstmt = conn.prepareStatement(sql);


			rs = pstmt.executeQuery();// 리절트셋에 대한 부가정보를 갖는 객체얻어오기
			rsmd = rs.getMetaData();// 컬럼명 테이블에 셋팅

			int colCount = rsmd.getColumnCount();
			String t[] = new String[colCount + 1];
			t[0] = "NO";

			for (int i = 1; i <= colCount; i++) {
				String colName = rsmd.getColumnName(i);
				t[i] = colName;
			}

			for(int i=0; i<=t.length-1; i++){
				System.out.println(t[i]);
			}

			model = new DefaultTableModel(t,0);
			scrollPane.setViewportView(getTable());

			int k=0; 
			while (rs.next()) {
				v= new Vector<String>();
				k=k+1;
				v.add(""+k);
				for (int j = 1; j <= colCount; j++) {
					v.add(rs.getString(j));
				}
				model.addRow(v);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

	}


	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("전체 조회");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					columnadd();


				}
			});
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent e) {
					
					if(pm.mUpdate == null){
						pm.openupdate();
					}else{
						pm.mUpdate.setVisible(true);
					}

					
					int a=table.getSelectedRow();
					
					String c = (String)table.getValueAt(a, 1);
					String h = (String)table.getValueAt(a, 4);
					String i = (String)table.getValueAt(a, 3);
					String j = (String)table.getValueAt(a, 5);
					String t = (String)table.getValueAt(a, 6);
					String p = (String)table.getValueAt(a, 7);
					String pn = (String)table.getValueAt(a, 8);
					String em = (String)table.getValueAt(a, 9);
					String ad = (String)table.getValueAt(a, 10);
					String py = (String)table.getValueAt(a, 11);
					
					
					pm.mUpdate.setData(c,h,i,j,t,p,pn,em,ad,py);
					
					
					
					
				}
			});
			tSoter = new TableRowSorter(model);
			table.setRowSorter(tSoter);
		}
		return table;
	}
}
